package com.example.fragment_work;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private LeftFragment leftFragment;
    private TextView tv_recommend,tv_must_buy;
    private RightFragment rightFragment;

    private String[] names1={"爆款*肥牛鱼豆腐骨肉相链三荤五素一份米饭","豪华双人套餐","【热销】双人套餐（含两份米饭）"};
    private String[] sales1={"月售520 好评度 80%","月售 184 好评度 68%","月售 114 好评度 60%"};
    private String[] prices1={"￥ 23","￥ 41","￥ 32"};
    private int[] imgs1={R.drawable.dish_1,R.drawable.dish_2,R.drawable.dish_3};

    private  String[] names2={"蔬菜主义1人套餐","2人经典套餐","3人经典套餐"};
    private String[] sales2={"月售26 好评度 70%","月售12 好评度 50%","月售4 好评度 40%"};
    private String[] prices2={"￥ 44","￥ 132","￥ 180"};
    private int[] imgs2={R.drawable.signature_dish_1,R.drawable.signature_dish_2,R.drawable.signature_dish_3};
   private Map<String,List<FoobBeen>> map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setData();
        init();
        clickEvent();
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

    }
    private void init(){
        fragmentManager=getSupportFragmentManager();
        leftFragment=(LeftFragment) fragmentManager.findFragmentById(R.id.left);
        tv_recommend=leftFragment.getView().findViewById(R.id.tv_recommend);
        tv_must_buy=leftFragment.getView().findViewById(R.id.tv_must_buy);
    }
    private void setData(){
        map=new HashMap<>();
        List<FoobBeen> list1=new ArrayList<>();
        List<FoobBeen> list2=new ArrayList<>();
        for (int i = 0; i < names1.length; i++) {
            FoobBeen foobBeen=new FoobBeen();
            foobBeen.setName(names1[i]);
            foobBeen.setSales(sales1[i]);
            foobBeen.setPrice(prices1[i]);
            foobBeen.setImg(imgs1[i]);
            list1.add(foobBeen);
        }
        map.put("1",list1);
        for (int i = 0; i < names2.length; i++) {
            FoobBeen foobBeen=new FoobBeen();
            foobBeen.setName(names2[i]);
            foobBeen.setSales(sales2[i]);
            foobBeen.setPrice(prices2[i]);
            foobBeen.setImg(imgs2[i]);
            list2.add(foobBeen);
        }
        map.put("2",list2);
    }
    private void clickEvent(){
        tv_recommend.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
               switchData(map.get("1"));
               tv_recommend.setBackgroundColor(Color.WHITE);
               tv_must_buy.setBackgroundResource(R.color.gray);
            }
        });
        tv_must_buy.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                switchData(map.get("2"));
                tv_must_buy.setBackgroundColor( Color.WHITE);
                tv_recommend.setBackgroundResource(R.color.gray);
            }
        });
        switchData(map.get("1"));
    }
    private void switchData(List<FoobBeen> list){
        fragmentManager=getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction();
        rightFragment=new RightFragment().getInstance(list);
        fragmentTransaction.replace(R.id.right,rightFragment);
        fragmentTransaction.commit();
    }
}